package org.ezplatform.office.meeting.common.service;

import java.util.List;
import java.util.Map;

import org.ezplatform.cloud.api.system.user.UserApiService;
import org.ezplatform.dingisv.util.DingMessageUtil;
import org.ezplatform.office.meeting.info.entity.ApplyRecord;
import org.ezplatform.office.meeting.info.service.ApplyRecordService;
import org.ezplatform.util.GlobalConstant;
import org.springframework.beans.factory.annotation.Autowired;

public class SendMessageService {

    private String msgtype = "oa";
    private String module = "meeting";
    
    @Autowired
    private ApplyRecordService applyRecordService;

    @Autowired
    private UserApiService userApiService;
    
    @Autowired
    private DingMessageUtil dingMessageUtil;
    
    /**
     * 发送消息 短信消息
     * @param applyRecord 会议信息
     * @param noticeTypeList 提醒方式  0:短信   1:邮件  2:消息 
     * @param applyRecordType 会议信息type 1:会前提醒
     * @param token 
     */
    public void send(ApplyRecord applyRecord, List<String> noticeTypeList, int applyRecordType, String token) {
        for(String noticeType : noticeTypeList){
            if("0".equals(noticeType)){
                this.sendMessage(applyRecord,applyRecordType,token);
            }else if("1".equals(noticeType)){
                this.sendEmail(applyRecord,applyRecordType,token);
            }else if("2".equals(noticeType)){
                this.sendInformation(applyRecord,applyRecordType,token);
            }
        }
    }

    /**
     * 消息
     * @param applyRecord
     * @param applyRecordType
     * @param token 
     */
    public void sendInformation(ApplyRecord applyRecord, int applyRecordType, String token) {
        if(applyRecordType==1){//会前提醒
            this.informationBefore(applyRecord,applyRecordType,token);
        }else if(applyRecordType==2){//同意申请会议
            this.informationAgreen(applyRecord,applyRecordType,token);
        }else if(applyRecordType==3){//取消会议
            this.informationReject(applyRecord,applyRecordType,token);
        }else if(applyRecordType==4){//会议纪要
            this.informationSummary(applyRecord,applyRecordType,token);
        }
    }

    private void informationBefore(ApplyRecord applyRecord, int applyRecordType, String token) {
        List<String> list = applyRecordService.getApplyRecordUserList(applyRecord,token);
        if(list!=null && list.size()>0){
            this.sendInformationToApp(module, msgtype, list, applyRecord, token, applyRecordType);
        }
    }
    
    private void informationAgreen(ApplyRecord applyRecord, int applyRecordType, String token) {
        List<String> list = applyRecordService.getApplyRecordUserList(applyRecord,token);
        if(list!=null && list.size()>0){
            this.sendInformationToApp(module, msgtype, list, applyRecord, token, applyRecordType);
        }
    }
    
    private void informationReject(ApplyRecord applyRecord, int applyRecordType, String token) {
        List<String> list = applyRecordService.getApplyRecordUserList(applyRecord,token);
        if(list!=null && list.size()>0){
            this.sendInformationToApp(module, msgtype, list, applyRecord, token, applyRecordType);
        }
    }
    
    private void informationSummary(ApplyRecord applyRecord,int applyRecordType, String token) {
        List<String> list = applyRecordService.getApplyRecordUserList(applyRecord,token);
        if(list!=null && list.size()>0){
            this.sendInformationToApp(module, msgtype, list, applyRecord, token, applyRecordType);
        }
    }

    /**
     * 邮件
     * @param applyRecord
     * @param applyRecordType
     * @param token 
     */
    public void sendEmail(ApplyRecord applyRecord, int applyRecordType, String token) {
        
    }

    /**
     * 短信
     * @param applyRecord
     * @param applyRecordType
     * @param token 
     */
    public void sendMessage(ApplyRecord applyRecord, int applyRecordType, String token) {
        
    }
    
    public void sendInformationToApp(String module,String msgtype, List<String> userIdList, ApplyRecord applyRecord,String token,int type){
        try{
            String corpId = applyRecord.getCorpId();
            //用户信息查询
            String userIdStr = "";
            //申请会议id
            String aplayRecordId = applyRecord.getId();
          //发送信息
            String message = "";
            String messageUrl = "";
            //链接ip
            String webUrl = "";
            long appId = 0L;
            String appIdString = GlobalConstant.getConfig((new StringBuilder()).append("dingisv.module.").append(module).append(".appid").toString());
            if(appIdString != null && !"".equals(appIdString)){
                appId = Long.parseLong(appIdString);
            }
            webUrl = GlobalConstant.getConfig((new StringBuilder()).append("web.url").toString());
            for(String userId : userIdList){
                Map<String,Object> userMap = this.userApiService.getUser(userId, corpId, "", token);
                userIdStr = String.valueOf(userMap.get("ddUId"));
                messageUrl = webUrl+"/static/dingding/document/index.html"
                    + "?appid=" + appId
                    + "&corpid=" + corpId
                    + "&dd_nav_bgcolor=FF5E97F6#/document/shouwen_info"
                    + "/" + aplayRecordId
                    + "/" + applyRecord.getName();
                message = "{\"message_url\":\""+messageUrl+"\","
                    +"\"head\": {\"bgcolor\":\"FFBBBBBB\",\"text\": \"公文\"},"
                    +"\"body\": {\"title\":\""+"111"+"\","
                    +"\"form\": [{\"key\": \"文号:\",\"value\": \""+"333"+"\"},{\"key\":\"拟稿单位:\",\"value\": \""+"444"+"\"},{\"key\": \"分发日期:\",\"value\": \""+"5555"+"\"}]"
                    +"}}";
                dingMessageUtil.sendMessage(corpId, module, userIdStr, null, msgtype, message);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    
}